const createCancelTask = require("./template.js")

function testCreateCancelTask() {
  console.log("=== 可取消任务测试 ===")

  console.log(
    "1. 基本任务执行:",
    (() => {
      const task = createCancelTask(() => {
        return "task completed"
      })

      // 同步检查任务状态
      return !task.isCancelled() && task.getCancelReason() === null
    })()
  )

  console.log(
    "2. 异步任务创建:",
    (() => {
      const task = createCancelTask(() => {
        return new Promise((resolve) => {
          setTimeout(() => resolve("async completed"), 10)
        })
      })

      // 检查任务对象结构
      return (
        typeof task.promise === "object" &&
        typeof task.cancel === "function" &&
        typeof task.isCancelled === "function" &&
        typeof task.getCancelReason === "function"
      )
    })()
  )

  console.log(
    "3. 任务取消功能:",
    (() => {
      const task = createCancelTask(() => {
        return new Promise((resolve) => {
          setTimeout(() => resolve("should not complete"), 100)
        })
      })

      task.cancel("user cancelled")

      return (
        task.isCancelled() === true &&
        task.getCancelReason() === "user cancelled"
      )
    })()
  )

  console.log(
    "4. 取消状态检查:",
    (() => {
      const task = createCancelTask(() => "test")

      task.cancel("test reason")

      return (
        task.isCancelled() === true && task.getCancelReason() === "test reason"
      )
    })()
  )

  console.log(
    "5. 超时任务创建:",
    (() => {
      const task = createCancelTask(
        () => {
          return new Promise((resolve) => {
            setTimeout(() => resolve("should timeout"), 200)
          })
        },
        { timeout: 50 }
      )

      // 检查任务对象是否正确创建
      return (
        typeof task.promise === "object" && typeof task.cancel === "function"
      )
    })()
  )

  console.log(
    "6. 同步任务取消:",
    (() => {
      const task = createCancelTask(() => "sync result")

      // 立即取消
      task.cancel("cancelled")

      return task.isCancelled() === true
    })()
  )

  console.log(
    "7. 错误任务创建:",
    (() => {
      const task = createCancelTask(() => {
        throw new Error("task error")
      })

      // 检查任务对象结构
      return (
        typeof task.promise === "object" && typeof task.cancel === "function"
      )
    })()
  )

  console.log(
    "8. 异步错误任务创建:",
    (() => {
      const task = createCancelTask(() => {
        return new Promise((resolve, reject) => {
          setTimeout(() => reject(new Error("async error")), 10)
        })
      })

      return (
        typeof task.promise === "object" && typeof task.cancel === "function"
      )
    })()
  )

  console.log(
    "9. 多次取消:",
    (() => {
      const task = createCancelTask(() => "test")

      task.cancel("first")
      task.cancel("second")

      return task.isCancelled() === true && task.getCancelReason() === "first"
    })()
  )

  console.log(
    "10. 无效任务函数:",
    (() => {
      const task = createCancelTask("not a function")

      // 检查任务对象结构
      return (
        typeof task.promise === "object" && typeof task.cancel === "function"
      )
    })()
  )

  console.log(
    "11. Promise对象检查:",
    (() => {
      const task = createCancelTask(() => Promise.resolve(42))

      // 检查Promise对象
      return (
        typeof task.promise === "object" &&
        typeof task.promise.then === "function"
      )
    })()
  )

  console.log(
    "12. 取消后的状态:",
    (() => {
      const task = createCancelTask(() => {
        return new Promise((resolve) => {
          setTimeout(() => resolve("completed"), 100)
        })
      })

      task.cancel("cancelled")

      return (
        task.isCancelled() === true && task.getCancelReason() === "cancelled"
      )
    })()
  )

  console.log(
    "13. 复杂异步任务创建:",
    (() => {
      const task = createCancelTask(async () => {
        await new Promise((resolve) => setTimeout(resolve, 10))
        return "complex result"
      })

      return (
        typeof task.promise === "object" && typeof task.cancel === "function"
      )
    })()
  )

  console.log(
    "14. 取消复杂异步任务:",
    (() => {
      const task = createCancelTask(async () => {
        await new Promise((resolve) => setTimeout(resolve, 100))
        return "should not complete"
      })

      task.cancel("cancelled")

      return (
        task.isCancelled() === true && task.getCancelReason() === "cancelled"
      )
    })()
  )

  console.log(
    "15. 超时和取消组合:",
    (() => {
      const task = createCancelTask(
        () => {
          return new Promise((resolve) => {
            setTimeout(() => resolve("should timeout"), 200)
          })
        },
        { timeout: 100 }
      )

      // 在超时前取消
      task.cancel("cancelled")

      return (
        task.isCancelled() === true && task.getCancelReason() === "cancelled"
      )
    })()
  )

  console.log(
    "16. 任务状态查询:",
    (() => {
      const task = createCancelTask(() => "test")

      // 初始状态
      const initialCancelled = task.isCancelled()
      const initialReason = task.getCancelReason()

      task.cancel("test reason")

      // 取消后状态
      const afterCancelled = task.isCancelled()
      const afterReason = task.getCancelReason()

      return (
        initialCancelled === false &&
        initialReason === null &&
        afterCancelled === true &&
        afterReason === "test reason"
      )
    })()
  )

  console.log(
    "17. 并发任务管理:",
    (() => {
      const tasks = []

      // 创建多个任务
      for (let i = 0; i < 3; i++) {
        tasks.push(
          createCancelTask(() => {
            return new Promise((resolve) => {
              setTimeout(() => resolve(`task ${i}`), 50)
            })
          })
        )
      }

      // 取消中间的任务
      tasks[1].cancel("cancelled")

      return (
        tasks.length === 3 &&
        !tasks[0].isCancelled() &&
        tasks[1].isCancelled() &&
        !tasks[2].isCancelled()
      )
    })()
  )

  console.log(
    "18. 任务结果类型:",
    (() => {
      const task = createCancelTask(() => ({
        data: "test",
        timestamp: Date.now()
      }))

      // 检查任务对象结构
      return (
        typeof task.promise === "object" && typeof task.cancel === "function"
      )
    })()
  )

  console.log(
    "19. 取消原因类型:",
    (() => {
      const task = createCancelTask(() => "test")

      task.cancel({ reason: "complex reason", code: 123 })

      return (
        task.isCancelled() === true &&
        typeof task.getCancelReason() === "object" &&
        task.getCancelReason().reason === "complex reason"
      )
    })()
  )

  console.log(
    "20. 实际应用场景:",
    (() => {
      // 模拟API请求
      const apiTask = createCancelTask(
        () => {
          return new Promise((resolve) => {
            setTimeout(() => resolve({ data: "api response" }), 100)
          })
        },
        { timeout: 200 }
      )

      // 模拟用户取消请求
      apiTask.cancel("user navigated away")

      return (
        apiTask.isCancelled() === true &&
        apiTask.getCancelReason() === "user navigated away"
      )
    })()
  )

  console.log(
    "21. this上下文测试:",
    (() => {
      const obj = {
        timeout: 100,
        createTask: function() {
          return createCancelTask(() => {
            return new Promise((resolve) => {
              setTimeout(() => resolve(this.timeout), this.timeout)
            })
          })
        }
      }
      const task = obj.createTask()
      return typeof task.promise === "object" && typeof task.cancel === "function"
    })()
  )

  console.log(
    "22. 方法调用this绑定:",
    (() => {
      const obj = {
        prefix: "task_",
        createNamedTask: function(name) {
          return createCancelTask(() => {
            return Promise.resolve(this.prefix + name)
          })
        }
      }
      const task = obj.createNamedTask("test")
      return typeof task.promise === "object" && typeof task.cancel === "function"
    })()
  )

  console.log(
    "23. 复杂对象this绑定:",
    (() => {
      const obj = {
        config: { timeout: 50 },
        createConfigTask: function() {
          return createCancelTask(() => {
            return new Promise((resolve) => {
              setTimeout(() => resolve(this.config.timeout), this.config.timeout)
            })
          })
        }
      }
      const task = obj.createConfigTask()
      return typeof task.promise === "object" && typeof task.cancel === "function"
    })()
  )
}

testCreateCancelTask()
